module BREBG {
    export class PlayMediatorBREBG extends BaseMediator {
        public static NAME: string = "PlayMediatorBREBG";
        public constructor(viewComponent: any = null) {
            super(PlayMediatorBREBG.NAME, viewComponent);
        }
        public listNotificationInterests(): Array<any> {
            return [
                PanelNotify.HALL.STAGE_RESIZE,
                PanelNotify.BREBG.CLOSE_PLAY_GAME,
                PanelNotify.BREBG.JOIN_ROOM,
                PanelNotify.BREBG.START_GAME,
                PanelNotify.BREBG.BET_RECEIVE,
                PanelNotify.BREBG.GAME_OVER,
                PanelNotify.BREBG.RECEIVE_LUDAN,
                PanelNotify.BREBG.GET_SERIAL_NUMBER,
                PanelNotify.BREBG.WAIT_NEXT_GAME,
                PanelNotify.BREBG.WAIT_START_GAME,
                PanelNotify.BREBG.DATA_RECOVERY,
                PanelNotify.BREBG.RECEIVE_USER_LIST
            ];
        }
        private playPanel: PlayPanelBREBG;
        public handleNotification(notification: puremvc.INotification): void {
            switch (notification.getName()) {
                case PanelNotify.HALL.STAGE_RESIZE:
                    if (this.playPanel) {
                        this.playPanel.width = GameConfig.curWidth()
                        this.playPanel.height = GameConfig.curHeight()
                    }
                    break;
                case PanelNotify.BREBG.CLOSE_PLAY_GAME: {
                    this.destroy()
                    break;
                }
                case PanelNotify.BREBG.JOIN_ROOM:
                    if (this.playPanel && this.playPanel.parent) {
                        this.initUserInfo();
                    } else {
                        this.playPanel = null;
                        this.playPanel = new PlayPanelBREBG();
                        this.showUI(this.playPanel, 2, 5);
                        game.AppFacade.instance.sendNotification(PanelNotify.HALL.JOIN_GAME_BY_HALL)
                    }
                    break;
                case PanelNotify.BREBG.START_GAME:
                    this.gameStart()
                    break;
                case PanelNotify.BREBG.BET_RECEIVE:
                    this.betReceive()
                    break;
                case PanelNotify.BREBG.GAME_OVER:
                    this.gameOver()
                    break;
                case PanelNotify.BREBG.RECEIVE_LUDAN:
                    this.shouLuDan();
                    break;
                case PanelNotify.BREBG.GET_SERIAL_NUMBER:
                    this.initRoomInfo();
                    break;
                case PanelNotify.BREBG.WAIT_NEXT_GAME:
                    this.waitNextGame();
                    break;
                case PanelNotify.BREBG.WAIT_START_GAME:
                    this.waitStartGame();
                    break;
                case PanelNotify.BREBG.DATA_RECOVERY:
                    this.dataRecovery();
                    break;
                case PanelNotify.BREBG.RECEIVE_USER_LIST:
                    this.receiveUserList();
                    break;
            }
        }
        /**
        * 初始化面板ui
        */
        private bet_chip = [100, 1000, 5000, 10000, 50000];//筹码规格
        private betImgArr: Array<Array<eui.Image>> = [[], [], []];//3个筹码区域的图片都放在这个数组，结算动画匹配输家数组种图片飞向庄家，庄家赔钱飞向赢家数组
        private fapaiXY = [];//0号到5号玩家的牌坐标  每1轮发牌X+50
        private timeOutArr = [];
        private intervalArr = [];
        private chipArr = [];//本局下的筹码
        private upChipArr = [];//上局下的筹码
        private globLook: boolean = false;//结算过程金额不准变动

        public initUI(): void {
            if (InstructionsEnum.SOUND_GAME != InstructionsEnum.GAME_SOUND_NAME.BREBG) {
                SoundManage.stopYinYue()
                InstructionsEnum.SOUND_GAME = InstructionsEnum.GAME_SOUND_NAME.BREBG
                SoundManage.startYinYueLoad();
            }
            //将滚动公告转移到当前面板
            EffectUtils.moveNoticeToPanel(this.playPanel.noticeGroup)

            this.playPanel.menu_button.addEventListener(egret.TouchEvent.TOUCH_TAP, this.menu_click, this)
            this.playPanel.menu_fankui.addEventListener(egret.TouchEvent.TOUCH_TAP, this.fankuiClick, this)
            this.playPanel.menu_jilu.addEventListener(egret.TouchEvent.TOUCH_TAP, this.jiluClick, this)
            this.playPanel.menu_set.addEventListener(egret.TouchEvent.TOUCH_TAP, this.setClick, this)
            this.playPanel.menu_help.addEventListener(egret.TouchEvent.TOUCH_TAP, this.helpClick, this)
            this.playPanel.menu_exit.addEventListener(egret.TouchEvent.TOUCH_TAP, this.exitClick, this)
            this.playPanel.user_Other.addEventListener(egret.TouchEvent.TOUCH_TAP, this.userListClic, this)
            this.playPanel.BREBG_xutou.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onContinueBtnClick, this);
            //【点击中发白下注】
            this.playPanel.BREBG_rect_1.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onRectClick, this);
            this.playPanel.BREBG_rect_2.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onRectClick, this);
            this.playPanel.BREBG_rect_3.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onRectClick, this);
            this.playPanel.bet_button_group.addEventListener(egret.TouchEvent.TOUCH_TAP, this.betGroupClick, this)
            this.fapaiXY = [
                { x: this.playPanel.BREBG_0_chess0.x, y: this.playPanel.BREBG_0_chess0.y },
                { x: this.playPanel.BREBG_1_chess0.x, y: this.playPanel.BREBG_1_chess0.y },
                { x: this.playPanel.BREBG_2_chess0.x, y: this.playPanel.BREBG_2_chess0.y },
                { x: this.playPanel.BREBG_3_chess0.x, y: this.playPanel.BREBG_3_chess0.y }
            ]
        }
        /**
         * 初始化面板数据
         */
        public initData(): void {
            this.globLook = false
            this.clearData();
            this.initRoomInfo();
            this.initUserInfo();
        }
        public destroy(): void {
            Global.removeInterval(this.intervalArr)
            Global.removeTimeOut(this.timeOutArr)
            game.AppFacade.instance.sendNotification(PanelNotify.HALL.CLOSE_HELP);
            game.AppFacade.instance.sendNotification(PanelNotify.HALL.CLOSE_USER_LIST);
            this.closePanel(1);
        }
        //初始化房间信息
        private initRoomInfo() {
            this.playPanel.BREBG_gold_1.text = "" + BrebgVO.getVo().betNumAdds_0 / 100;
            this.playPanel.BREBG_gold_2.text = "" + BrebgVO.getVo().betNumAdds_1 / 100;
            this.playPanel.BREBG_gold_3.text = "" + BrebgVO.getVo().betNumAdds_2 / 100;
            this.playPanel.BREBG_gold_1_my.text = "" + BrebgVO.getVo().betNumAdds_0_my / 100;
            this.playPanel.BREBG_gold_2_my.text = "" + BrebgVO.getVo().betNumAdds_1_my / 100;
            this.playPanel.BREBG_gold_3_my.text = "" + BrebgVO.getVo().betNumAdds_2_my / 100;
            this.playPanel.bianhao.text = "局号：" + BrebgVO.getVo().gameNumbering;

            // this.playPanel.BREBG_userName_zhuang.text = "系统坐庄";
            // this.playPanel.BREBG_gold_zhuang.text = BrebgVO.getVo().lBankerScore + "";
        }
        /**
        * 初始化个人信息
        */
        private initUserInfo() {
            //玩家信息
            this.playPanel.BREBG_userName.text = Global.cutString(GameVO.getVo().szNickName + "");
            this.playPanel.BREBG_user.source = "special_" + GameVO.getVo().wFaceID % 15;
            if (BrebgVO.getVo().structUserList) {
                this.playPanel.userOther_num.text = BrebgVO.getVo().structUserList.length + ""
            }
            if (!this.globLook) {
                this.playPanel.BREBG_gold.text = BrebgVO.getVo().lUserScore / 100 + "";
            }
        }
        //初始化页面数据
        private clearData() {
            this.globLook = false
            Global.removeTimeOut(this.timeOutArr)
            Global.removeInterval(this.intervalArr)
            this.IsUserScore(0)

            //清空分数
            this.playPanel.score_zhuang.text = "";
            this.playPanel.score_user.text = "";
            this.playPanel.score_userList.text = "";
            this.playPanel.xiazhu_tips.source = "";
            //【清空牛类型】
            this.playPanel.BREBG_0_niuType.source = "";
            this.playPanel.BREBG_1_niuType.source = "";
            this.playPanel.BREBG_2_niuType.source = "";
            this.playPanel.BREBG_3_niuType.source = "";
            this.playPanel.zhuang_chess_type.visible = false

            //下注信息
            this.playPanel.BREBG_gold_1.text = "";
            this.playPanel.BREBG_gold_2.text = "";
            this.playPanel.BREBG_gold_3.text = "";
            this.playPanel.BREBG_gold_1_my.text = "";
            this.playPanel.BREBG_gold_2_my.text = "";
            this.playPanel.BREBG_gold_3_my.text = "";
            for (var i = 0; i < 4; i++) {
                for (var k = 0; k < 2; k++) {
                    this.playPanel["BREBG_" + i + "_chess" + k].x = 655;
                    this.playPanel["BREBG_" + i + "_chess" + k].y = 330;
                    this.playPanel["BREBG_" + i + "_chess" + k].source = "";
                    this.playPanel["BREBG_" + i + "_chess" + k].scaleX = 1;
                }
            }
            this.playPanel.BREBG_BetPanel.removeChildren();
            this.betImgArr = [[], [], [], []];
        }
        //游戏开始
        private gameStart() {
            this.clearData();
            SoundManage.startYinxiaoLoad("GAME_START_mp3");
            this.chipArr.splice(0, this.chipArr.length);
            this.playPanel.BREBG_xutou.enabled = true;
            this.playPanel.BREBG_xutou.name = "";
            var thisBack = this;
            thisBack.playPanel.title.source = "brnn_sourec_23"
            thisBack.playPanel.title.visible = true;
            egret.Tween.get(thisBack.playPanel.title).to({ x: GameConfig.curWidth() / 2 - thisBack.playPanel.title.width / 2 }, 200).wait(500).call(() => {
                SoundManage.startYinxiaoLoad("kaishixiazhu_mp3");
                thisBack.playPanel.timeOut.init(9, true)
                thisBack.IsUserScore(BrebgVO.getVo().allowlUserScore, false, true)

            }).to({ x: GameConfig.curWidth() }, 200).call(() => {
                thisBack.playPanel.title.visible = false;
                thisBack.playPanel.title.x = -(thisBack.playPanel.title.width);
                this.playPanel.xiazhu_tips.source = "timeState_xiazhu";
            })
            if (BrebgVO.getVo().gameNum >= 6) {
                let data = {
                    wTableID: BrebgVO.getVo().wTableId,
                    wChairID: BrebgVO.getVo().wChairId,
                    cbForceLeave: 1
                }
                SocketManager.sendMessage(InstructionsEnum.GAME_TYPE_NAME.BREBG, RegDataGamePublic.CMD_GP_LeaveGame, data, true);
                Global.tipShow("您已经连续 5 局未下注，被请离了游戏桌")
            } else if (BrebgVO.getVo().gameNum == 4) {
                Global.tipShow("您已经连续 3 局未下注，连续 5 局未下注将被请离游戏桌!")
            }
        }
        //下注返回
        private betReceive() {
            let startX, startY, rect: eui.Rect, arr: Array<eui.Image>;//筹码飞行动画起始位置，结束位置,结束区域宽高,装筹码的数组
            let betUser = BrebgVO.getVo().betUser;
            let betAdds = BrebgVO.getVo().betAdds;
            let betNum = BrebgVO.getVo().betNum;
            let isMy = false;
            if (betUser == BrebgVO.getVo().wChairId) {
                this.playPanel.BREBG_gold.text = BrebgVO.getVo().lUserScore / 100 + "";

                startX = this.playPanel.my_info.x + 60 - this.playPanel.BREBG_BetPanel.x
                startY = this.playPanel.my_info.y - 25 - this.playPanel.BREBG_BetPanel.y;
                isMy = true;
                let jbArr = {
                    betNum: betNum,
                    betAdds: betAdds
                };
                this.chipArr.push(jbArr);
            } else {
                startX = this.playPanel.user_Other.x - 25 - this.playPanel.BREBG_BetPanel.x;
                startY = this.playPanel.user_Other.y - 25 - this.playPanel.BREBG_BetPanel.y;
            }
            switch (betAdds) {
                case 1:
                    rect = this.playPanel.fanwei_1
                    arr = this.betImgArr[0];
                    break;
                case 2:
                    rect = this.playPanel.fanwei_2
                    arr = this.betImgArr[1];
                    break;
                case 3:
                    rect = this.playPanel.fanwei_3
                    arr = this.betImgArr[2];
                    break;
            }
            let imgArr = this.qiegeJinbi(betNum);
            for (let y = 0; y < imgArr.length; y++) {
                let img = betImgPool.getFreeObject("bet_" + (imgArr[y] / 100))
                img.x = startX;
                img.y = startY;
                img.width = 50;
                img.height = 50;
                img.visible = false;
                img.name = betUser + "";//记住是谁下的注，庄家赔钱时这个注要飞向下注那个玩家
                this.playPanel.BREBG_BetPanel.addChild(img);
                arr.push(img)
                egret.Tween.get(img).wait(isMy ? 0 : this.random_num(50, 700))
                    .to({ visible: true })
                    .to({ x: rect.x + rect.width * Math.random(), y: rect.y + rect.height * Math.random() }, 500, egret.Ease.circInOut).call((img: eui.Image) => {
                        egret.Tween.removeTweens(img);
                    }, this, [img]);
                SoundManage.startYinxiaoLoad("ADD_GOLD_mp3");
            }
            this.initRoomInfo();
        }
        //游戏结算
        private gameOver() {
            this.globLook = true;

            if (this.chipArr.length == 0) {//若本局没有续押也没有手动下注，则下一次 上局筹码 为空
                this.upChipArr.splice(0, this.upChipArr.length);
            } else {
                this.upChipArr = [...this.chipArr];//本局有下注则记录为 “下一次 上局筹码 ” 上一局的不保留
            }
            this.playPanel.BREBG_xutou.enabled = false;
            SoundManage.startYinxiaoLoad("kaishibipai_mp3");
            this.IsUserScore(0);
            var thisBack = this;
            this.playPanel.xiazhu_tips.source = "timeState_fapai";
            thisBack.playPanel.timeOut.init(6)
            try {
                //停止下注动画
                var tingzhixiazhu = () => {
                    thisBack.playPanel.title.source = "brebg_texture_17"
                    thisBack.playPanel.title.visible = true;
                    egret.Tween.get(thisBack.playPanel.title).to({ x: GameConfig.curWidth() / 2 - thisBack.playPanel.title.width / 2 }, 200).wait(500).to({ x: GameConfig.curWidth() }, 200).call(() => {
                        thisBack.playPanel.title.visible = false;
                        thisBack.playPanel.title.x = -(thisBack.playPanel.title.width);
                        fapai();
                    })
                }
                //发牌
                var fapai = () => {
                    thisBack.playPanel.zhuang_chess_type.visible = true
                    let i = 0;//发到地几个人
                    let j = 0;//发到第几轮了
                    let t = egret.setInterval(() => {
                        let img: eui.Image = thisBack.playPanel["BREBG_" + i + "_chess" + j];
                        img.source = "brebg_chess_back";
                        egret.Tween.get(img).to({ x: thisBack.fapaiXY[i].x + j * 80, y: thisBack.fapaiXY[i].y }, 200).call((img: eui.Image) => {
                            egret.Tween.removeTweens(img);
                        }, this, [img]);
                        SoundManage.startYinxiaoLoad("SendCardSound_mp3");
                        i++;
                        if (i % 4 == 0) {
                            i = 0;
                            j++;
                        }
                        if (j >= 2) {//只发两轮后翻牌
                            egret.clearInterval(t);
                            fanpai();
                        }
                    }, this, 100)
                    thisBack.intervalArr.push(t)
                }
                //翻牌
                var fanpai = () => {
                    var j = 0;//第几轮了
                    var t = egret.setInterval(() => {
                        try {
                            var index = j;
                            for (let i = 0; i < 2; i++) {
                                let source = "brebg_chess_" + BrebgVO.getVo().poker_arr[index][i];
                                let img = thisBack.playPanel["BREBG_" + index + "_chess" + i];
                                EffectUtils.xuanZuanHuanTu(img, source);
                            }
                            let t2 = egret.setTimeout(() => {
                                thisBack.playPanel["BREBG_" + index + "_niuType"].source = "brebg_type_" + thisBack.getChessType(BrebgVO.getVo().poker_arr[index]);
                                thisBack.playPanel["BREBG_" + index + "_niuType"].anchorOffsetX = thisBack.playPanel["BREBG_" + index + "_niuType"].width / 2
                                thisBack.playPanel["BREBG_" + index + "_niuType"].anchorOffsetY = thisBack.playPanel["BREBG_" + index + "_niuType"].height / 2
                                EffectUtils.Stretch(thisBack.playPanel["BREBG_" + index + "_niuType"]);
                                SoundManage.startYinxiaoLoad("ErBaGang_" + thisBack.getChessType(BrebgVO.getVo().poker_arr[index]) + "_mp3");
                                egret.clearTimeout(t2);
                            }, this, 600)
                            thisBack.timeOutArr.push(t2);
                            j++;
                            if (j >= 4) {
                                egret.setTimeout(shouChouMa_zhuang, this, 700)
                                egret.clearInterval(t);
                                return;
                            }
                        } catch (err) {
                            egret.setTimeout(shouChouMa_zhuang, this, 400)
                            egret.clearInterval(t);
                            console.log("数据异常，终止翻牌动画", BrebgVO.getVo())
                        }
                    }, this, 1000)
                    thisBack.intervalArr.push(t)
                }
                let ZhuangBetAddsArr: Array<number> = [];//zhuang家要收取的筹码区域
                //收筹码-庄
                var shouChouMa_zhuang = () => {
                    this.playPanel.xiazhu_tips.source = "timeState_paijiang";
                    thisBack.playPanel.timeOut.init(BrebgVO.getVo().cbTimeLeave - 6)
                    try {
                        if (BrebgVO.getVo().score_arr[0] == 0) {
                            ZhuangBetAddsArr.push(0)
                        }
                        if (BrebgVO.getVo().score_arr[1] == 0) {
                            ZhuangBetAddsArr.push(1)
                        }
                        if (BrebgVO.getVo().score_arr[2] == 0) {
                            ZhuangBetAddsArr.push(2)
                        }

                        if (ZhuangBetAddsArr.length == 0) {//庄家通赔不收钱
                            SoundManage.startYinxiaoLoad("tongpei_mp3");
                            peiChouMa();
                        } else {
                            for (let i = 0; i < thisBack.betImgArr.length; i++) {
                                if (ZhuangBetAddsArr.indexOf(i) != -1) {
                                    let imgArr: Array<eui.Image> = thisBack.betImgArr[i];
                                    for (let j = 0; j < imgArr.length; j++) {
                                        egret.Tween.get(imgArr[j]).to({ x: 617, y: 195 }, 300).call((img: eui.Image) => {
                                            egret.Tween.removeTweens(img);
                                            if (thisBack.playPanel.BREBG_BetPanel.contains(img)) {
                                                thisBack.playPanel.BREBG_BetPanel.removeChild(img);
                                                betImgPool.returnObject(img)
                                            }
                                        }, this, [imgArr[j]])
                                        if (Math.random() * 100 < 5) {//筹码太多,声音随机次数播放,降低性能消耗
                                            let t = egret.setTimeout(() => {
                                                SoundManage.startYinxiaoLoad("ADD_GOLD_mp3");
                                                egret.clearTimeout(t)
                                            }, this, this.random_num(10, 500))
                                            thisBack.timeOutArr.push(t)
                                        }
                                    }
                                }
                            }
                            let t = egret.setTimeout(peiChouMa, this, 500)
                            thisBack.timeOutArr.push(t)
                        }
                    } catch (err) {
                        console.log("数据异常，终止收筹码", BrebgVO.getVo())
                    }
                }
                //赔筹码
                var peiChouMa = () => {
                    try {
                        if (ZhuangBetAddsArr.length == 3) {//庄家通吃不赔钱
                            SoundManage.startYinxiaoLoad("tongchi_mp3");
                            shouFen();
                        } else {
                            let rect: eui.Rect, betArr: Array<eui.Image>, score;//筹码飞行动画结束位置,装筹码的数组,赔钱数量
                            for (let i = 0; i < thisBack.betImgArr.length; i++) {
                                if (ZhuangBetAddsArr.indexOf(i) == -1) {//此区域要赔钱
                                    switch (i) {
                                        case 0:
                                            rect = this.playPanel.fanwei_1
                                            score = Math.abs(BrebgVO.getVo().betNumAdds_0);
                                            break;
                                        case 1:
                                            rect = this.playPanel.fanwei_2
                                            score = Math.abs(BrebgVO.getVo().betNumAdds_1);
                                            break;
                                        case 2:
                                            rect = this.playPanel.fanwei_3
                                            score = Math.abs(BrebgVO.getVo().betNumAdds_2);
                                            break;
                                    }
                                    betArr = thisBack.betImgArr[i];
                                    let imgArr: Array<any> = this.qiegeJinbi(score);
                                    for (let y = 0; y < imgArr.length; y++) {
                                        let img = betImgPool.getFreeObject("bet_" + (imgArr[y] / 100))
                                        img.x = 617;
                                        img.y = 195;
                                        img.width = 50;
                                        img.height = 50;
                                        this.playPanel.BREBG_BetPanel.addChild(img);
                                        betArr.push(img)
                                        egret.Tween.get(img).to({ x: rect.x + rect.width * Math.random(), y: rect.y + rect.height * Math.random() }, 350, egret.Ease.circInOut).call((img: eui.Image) => {
                                            egret.Tween.removeTweens(img);
                                        }, this, [img]);
                                        if (Math.random() < 0.2) {//筹码太多,声音随机次数播放,降低性能消耗
                                            let t = egret.setTimeout(() => {
                                                SoundManage.startYinxiaoLoad("ADD_GOLD_mp3");
                                                egret.clearTimeout(t);
                                            }, this, this.random_num(10, 350));
                                            thisBack.timeOutArr.push(t)
                                        }
                                    }
                                }
                            }
                            let t = egret.setTimeout(shouChouMa_xian, this, 500)
                            thisBack.timeOutArr.push(t)
                        }
                    } catch (err) {
                        console.log("数据异常，终止赔筹码", BrebgVO.getVo())
                    }
                }
                //收筹码-闲
                var shouChouMa_xian = () => {
                    try {
                        for (let i = 0; i < thisBack.betImgArr.length; i++) {
                            if (ZhuangBetAddsArr.indexOf(i) == -1) {
                                let imgArr = thisBack.betImgArr[i];
                                for (let i = 0; i < imgArr.length; i++) {
                                    var x = this.playPanel.user_Other.x - this.playPanel.BREBG_BetPanel.x
                                    var y = this.playPanel.user_Other.y - this.playPanel.BREBG_BetPanel.y
                                    if (imgArr[i].name == BrebgVO.getVo().wChairId + "") {
                                        x = this.playPanel.my_info.x + 60 - this.playPanel.BREBG_BetPanel.x
                                        y = this.playPanel.my_info.y - this.playPanel.BREBG_BetPanel.y
                                    }
                                    egret.Tween.get(imgArr[i]).wait(this.random_num(50, 700)).to({ x: x, y: y }, 350, egret.Ease.backInOut).call((img: eui.Image) => {
                                        egret.Tween.removeTweens(img);
                                        if (thisBack.playPanel.BREBG_BetPanel.contains(img)) {
                                            thisBack.playPanel.BREBG_BetPanel.removeChild(img);
                                            betImgPool.returnObject(img)
                                        }
                                    }, this, [imgArr[i]]);
                                    if (Math.random() * 100 < 5) {//筹码太多,声音随机次数播放,降低性能消耗
                                        let t = egret.setTimeout(() => {
                                            SoundManage.startYinxiaoLoad("ADD_GOLD_mp3");
                                            egret.clearTimeout(t)
                                        }, this, this.random_num(10, 350))
                                        thisBack.timeOutArr.push(t)
                                    }
                                }
                            }
                        }
                        let t = egret.setTimeout(shouFen, this, 500)
                        thisBack.timeOutArr.push(t)
                    } catch (err) {
                        console.log("数据异常，终止闲家收筹码", BrebgVO.getVo())
                    }
                }
                var shouFen = () => {
                    thisBack.playPanel.score_user.text = Global.getmeishufen(BrebgVO.getVo().score_my / 100);
                    thisBack.playPanel.score_zhuang.text = Global.getmeishufen(BrebgVO.getVo().score_zhuang / 100);
                    EffectUtils.flyObj(thisBack.playPanel.score_user)
                    EffectUtils.flyObj(thisBack.playPanel.score_zhuang)
                    thisBack.shouLuDan();
                    BrebgVO.getVo().game_state = 0
                    this.globLook = false
                    thisBack.initUserInfo();
                    thisBack.initRoomInfo();
                }
                tingzhixiazhu();
            } catch (e) {
                console.log("数据异常：", BrebgVO.getVo());
            }
        }
        //等待下局
        private waitNextGame() {
            this.playPanel.timeOut.init(BrebgVO.getVo().cbTimeLeave)
            this.playPanel.xiazhu_tips.source = "timeState_dengdai";
        }
        //等待开局
        private waitStartGame() {
            this.playPanel.timeOut.init(BrebgVO.getVo().cbTimeLeave)
            this.playPanel.xiazhu_tips.source = "timeState_zhunbei";
        }
        //推送场外玩家
        private receiveUserList() {
            if (BrebgVO.getVo().structUserList) {
                this.playPanel.userOther_num.text = BrebgVO.getVo().structUserList.length + ""
            }
        }
        //中途加入 游戏状态
        private dataRecovery() {
            this.playPanel.timeOut.init(BrebgVO.getVo().cbTimeLeave)
            this.playPanel.xiazhu_tips.source = "timeState_dengdai"
            if (BrebgVO.getVo().cbGameStatus == 100) {
                this.initRoomInfo();
                var goldArr = [
                    BrebgVO.getVo().betNumAdds_0,
                    BrebgVO.getVo().betNumAdds_1,
                    BrebgVO.getVo().betNumAdds_2,
                ]
                for (var i = 0; i < goldArr.length; i++) {
                    var rect: eui.Rect = this.playPanel["fanwei_" + (i + 1)]
                    var imgArr = this.betImgArr[i];
                    let betArr = this.qiegeJinbi(goldArr[i]);
                    for (let y = 0; y < betArr.length; y++) {
                        let img = betImgPool.getFreeObject("bet_" + (betArr[y] / 100))
                        img.x = rect.x + rect.width * Math.random()
                        img.y = rect.y + rect.height * Math.random()
                        img.width = 50;
                        img.height = 50;
                        this.playPanel.BREBG_BetPanel.addChild(img);
                        imgArr.push(img)
                    }
                }
                // this.playPanel.xiazhu_tips.source = "timeState_xiazhu";
                // this.IsUserScore(BrebgVO.getVo().allowlUserScore - BrebgVO.getVo().betNumAdds_my, false, true)
            }
        }
        //路单
        private shouLuDan() {
            if (!this.playPanel) {
                return;
            }
            try {
                var arr = BrebgVO.getVo().ludan;
                for (let a = 0; a < 3; a++) {
                    var ludan = <eui.Group>this.playPanel["BREBG_ludan_" + a];
                    for (let i = 0; i < ludan.numChildren; i++) {
                        if (ludan.getChildAt(i) instanceof eui.Image) {
                            betImgPool.returnObject(<eui.Image>ludan.getChildAt(i))
                        }
                    }
                    ludan.removeChildren();
                    var x = 0;
                    for (let i = 0; i < 6 && i < arr.length; i++) {
                        let img = betImgPool.getFreeObject(arr[i][a] == 1 ? "brebg_texture_20" : "brebg_texture_21")
                        img.x = x;
                        img.y = 10;
                        ludan.addChild(img)
                        if (i == 0) {
                            EffectUtils.Twinkle(img)
                        }
                        x += 32
                    }
                }
            } catch (e) {
                for (let a = 0; a < 3; a++) {
                    var ludan = <eui.Group>this.playPanel["BREBG_ludan_" + a];
                    for (let i = 0; i < ludan.numChildren; i++) {
                        if (ludan.getChildAt(i) instanceof eui.Image) {
                            betImgPool.returnObject(<eui.Image>ludan.getChildAt(i))
                        }
                    }
                    ludan.removeChildren();
                }
                EffectUtils.textFadeAway("路单数据异常")
            }

        }
        //点击筹码组，为处于下注状态给出提示
        private betGroupClick() {
            if (this.playPanel.xiazhu_tips.source != "timeState_xiazhu") {
                EffectUtils.textFadeAway("非下注状态，请等待...")
            }
        }
        //选择筹码
        private onChipClick(event: egret.TouchEvent): void {
            for (var i = 0; i < 5; i++) {
                if (this.playPanel["bet_" + i] != event.currentTarget) {
                    this.playPanel["bet_" + i].name = '';
                    egret.Tween.removeTweens(this.playPanel["bet_" + i + "_bg"]);
                    this.playPanel["bet_" + i + "_bg"].visible = false;
                    this.playPanel["bet_" + i + "_bg"].rotation = 0;
                } else {
                    if (event.currentTarget.name != 'ok') {
                        event.currentTarget.name = 'ok';
                        this.playPanel["bet_" + i + "_bg"].rotation = 0;
                        this.playPanel["bet_" + i + "_bg"].visible = true;
                        EffectUtils.rotationEffect(this.playPanel["bet_" + i + "_bg"]);
                        EffectUtils.playDim(event.currentTarget);
                        this.selectBet = event.currentTarget;
                    }
                }
            }
        }
        //点击桌面下注
        private onRectClick(event: egret.TouchEvent): void {
            let betArea;
            let BetNum = 0;
            let rect = event.currentTarget
            if (rect == this.playPanel.BREBG_rect_1) {
                betArea = "1";
                BetNum = BrebgVO.getVo().betNumAdds_0
            }
            if (rect == this.playPanel.BREBG_rect_2) {
                betArea = "2";
                BetNum = BrebgVO.getVo().betNumAdds_1
            }
            if (rect == this.playPanel.BREBG_rect_3) {
                betArea = "3";
                BetNum = BrebgVO.getVo().betNumAdds_2
            }
            let imgNum;
            for (var i = 0; i < 5; i++) {
                if (this.playPanel["bet_" + i].name == 'ok') {
                    imgNum = this.bet_chip[i];
                    break;
                }
            }
            if (!imgNum) {
                return;
            }
            egret.Tween.get(rect).to({ visible: false }).wait(100).to({ visible: true }).call(() => {
                egret.Tween.removeTweens(rect)
            }, this);

            if (BrebgVO.getVo().lUserScore < 5000) {
                EffectUtils.textFadeAway("抱歉，50 筹码以上才能下注!");
                this.IsUserScore(0)
                return;
            }
            if (BrebgVO.getVo().allowlUserScore < imgNum) {
                EffectUtils.textFadeAway("金币不足!");
                return;
            }
            if ((BetNum + imgNum) > BrebgVO.getVo().lAreaLimitScore) {
                EffectUtils.textFadeAway("区域限制最多下注" + BrebgVO.getVo().lAreaLimitScore / 100 + "元!");
                return;
            }
            //为防止网络延迟导致前端超量下注，在下注时就先扣除金币
            BrebgVO.getVo().allowlUserScore -= imgNum
            this.IsUserScore(BrebgVO.getVo().allowlUserScore, true)
            let obj = {
                cbJettonArea: betArea,
                lJettonScore: imgNum
            }
            SocketManager.sendMessage(InstructionsEnum.GAME_TYPE_NAME.BREBG, RegDataBrnn.CMD_GP_PlaceJetton, obj);
        }
        //续押
        private onContinueBtnClick(): void {
            SoundManage.startAnniuBtnLoad();
            if (this.playPanel.BREBG_xutou.name == "ok") {
                EffectUtils.textFadeAway("本局已续押!");
                return;
            }
            if (this.upChipArr.length == 0) {
                EffectUtils.textFadeAway("上局未下注，无法续押!");
                return;
            }
            if (BrebgVO.getVo().game_state == 0) {
                EffectUtils.textFadeAway("请等待下注时间!");
                return;
            }
            var thisBack = this;
            this.playPanel.BREBG_xutou.enabled = false;
            this.playPanel.BREBG_xutou.name = "ok";//记录本局已点击过续押，下局开局时恢复
            var fun: Function = function (time, betAdds, betNum) {
                let t = egret.setTimeout(function () {
                    let obj = {
                        cbJettonArea: betAdds,
                        lJettonScore: betNum
                    }
                    SocketManager.sendMessage(InstructionsEnum.GAME_TYPE_NAME.BREBG, RegDataBrnn.CMD_GP_PlaceJetton, obj);
                }, this, time)
                thisBack.timeOutArr.push(t);
            }
            let time = 0;
            let glod = 0;
            let glod2 = BrebgVO.getVo().lUserScore;//当前手头总金币，续押时超过这个数值不再续押
            for (let i in this.upChipArr) {
                time += 100;
                let betAdds = this.upChipArr[i].betAdds;
                let betNum = this.upChipArr[i].betNum;
                glod += betNum;
                if (glod <= glod2) {
                    fun(time, betAdds, betNum)
                } else {
                    EffectUtils.textFadeAway("金币不足，已中断续押");
                    break;
                }
            }
        }
        /**
      * 更改筹.码按钮状态
      * num:手头金额
      * xiazhu：是否下注后重置
      * startXiazhu：是否开局重置
      */
        private selectBet: eui.Button = null;
        public IsUserScore(num: number, xiazhu: boolean = false, startXiazhu: boolean = false) {
            var bet4 = Number(this.playPanel.bet_4.label) * 100
            var bet3 = Number(this.playPanel.bet_3.label) * 100
            var bet2 = Number(this.playPanel.bet_2.label) * 100
            var bet1 = Number(this.playPanel.bet_1.label) * 100
            var bet0 = Number(this.playPanel.bet_0.label) * 100
            //找出上次选中的筹码，若本轮金币不足导致筹码被取消选择，则筹码自动选择更低的
            let img = null;
            let modify = false;
            for (var i = 0; i < 5; i++) {
                if (this.playPanel['bet_' + i].name == 'ok') {
                    img = this.playPanel['bet_' + i];
                    break;
                }
            }
            if (num < bet4) {
                this.playPanel.bet_4.enabled = false;
                this.playPanel.bet_4.name = ""
                this.playPanel.bet_4.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
                this.playPanel.bet_4_bg.visible = false;
                this.playPanel.bet_4_bg.rotation = 0;
                egret.Tween.get(this.playPanel.bet_4_bg)
                if (img == this.playPanel.bet_4) {
                    modify = true;
                }
            } else {
                this.playPanel.bet_4.enabled = true;
                this.playPanel.bet_4.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
                this.playPanel.bet_4.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
            }
            if (num < bet3) {
                this.playPanel.bet_3.enabled = false;
                this.playPanel.bet_3.name = ""
                this.playPanel.bet_3.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
                this.playPanel.bet_3.name = '';
                this.playPanel.bet_3_bg.visible = false;
                this.playPanel.bet_3_bg.rotation = 0;
                egret.Tween.get(this.playPanel.bet_3_bg)
                if (img == this.playPanel.bet_3) {
                    modify = true;
                }
            } else {
                this.playPanel.bet_3.enabled = true;
                this.playPanel.bet_3.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
                this.playPanel.bet_3.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
            }
            if (num < bet2) {
                this.playPanel.bet_2.enabled = false;
                this.playPanel.bet_2.name = ""
                this.playPanel.bet_2.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
                this.playPanel.bet_2.name = '';
                this.playPanel.bet_2_bg.visible = false;
                this.playPanel.bet_2_bg.rotation = 0;
                egret.Tween.get(this.playPanel.bet_2_bg)
                if (img == this.playPanel.bet_2) {
                    modify = true;
                }
            } else {
                this.playPanel.bet_2.enabled = true;
                this.playPanel.bet_2.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
                this.playPanel.bet_2.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
            }
            if (num < bet1) {
                this.playPanel.bet_1.enabled = false;
                this.playPanel.bet_1.name = ""
                this.playPanel.bet_1.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
                this.playPanel.bet_1.name = '';
                this.playPanel.bet_1_bg.visible = false;
                this.playPanel.bet_1_bg.rotation = 0;
                egret.Tween.get(this.playPanel.bet_1_bg)
                if (img == this.playPanel.bet_1) {
                    modify = true;
                }
            } else {
                this.playPanel.bet_1.enabled = true;
                this.playPanel.bet_1.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
                this.playPanel.bet_1.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
            }
            if (num < bet0) {
                this.playPanel.bet_0.enabled = false;
                this.playPanel.bet_0.name = ""
                this.playPanel.bet_0.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
                this.playPanel.bet_0.name = '';
                this.playPanel.bet_0_bg.visible = false;
                this.playPanel.bet_0_bg.rotation = 0;
                egret.Tween.get(this.playPanel.bet_0_bg)
                if (img == this.playPanel.bet_0) {
                    modify = true;
                }
            } else {
                this.playPanel.bet_0.enabled = true;
                this.playPanel.bet_0.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
                this.playPanel.bet_0.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onChipClick, this);
            }
            //因下注导致钱不够筹码更换
            if ((xiazhu && modify)) {
                if (num >= bet4) {
                    this.playPanel.bet_4.name = "ok";
                    this.playPanel.bet_4_bg.visible = true;
                    EffectUtils.rotationEffect(this.playPanel.bet_4_bg);
                    this.selectBet = this.playPanel.bet_4;
                } else if (num >= bet3) {
                    this.playPanel.bet_3.name = "ok";
                    this.playPanel.bet_3_bg.visible = true;
                    EffectUtils.rotationEffect(this.playPanel.bet_3_bg);
                    this.selectBet = this.playPanel.bet_3;
                } else if (num >= bet2) {
                    this.playPanel.bet_2.name = "ok";
                    this.playPanel.bet_2_bg.visible = true;
                    EffectUtils.rotationEffect(this.playPanel.bet_2_bg);
                    this.selectBet = this.playPanel.bet_2;
                } else if (num >= bet1) {
                    this.playPanel.bet_1.name = "ok";
                    this.playPanel.bet_1_bg.visible = true;
                    EffectUtils.rotationEffect(this.playPanel.bet_1_bg);
                    this.selectBet = this.playPanel.bet_1;
                } else if (num >= bet0) {
                    this.playPanel.bet_0.name = "ok";
                    this.playPanel.bet_0_bg.visible = true;
                    EffectUtils.rotationEffect(this.playPanel.bet_0_bg);
                    this.selectBet = this.playPanel.bet_0;
                }
                // if (num < 100) {
                //     if (InstructionsEnum.LEVEL_GAME == 1 || InstructionsEnum.LEVEL_GAME == 4  || InstructionsEnum.LEVEL_GAME == 10) {
                //         EffectUtils.textFadeAway("金币不足，下注总额不能超出携带总额的1/4");
                //     } else {
                //         EffectUtils.textFadeAway("金币不足，下注总额不能超出携带总额的1/10");
                //     }
                // } else {
                EffectUtils.textFadeAway("金币不足，已自动切换筹码");
                // }
            }
            //新开局时上一局最后选中的筹码如果这一局依然可用则自动选中
            if (startXiazhu && this.selectBet != null) {
                if (num >= bet4 && this.selectBet == this.playPanel.bet_4) {
                    this.playPanel.bet_4.name = "ok"
                    this.playPanel.bet_4_bg.rotation = 0;
                    this.playPanel.bet_4_bg.visible = true;
                    EffectUtils.rotationEffect(this.playPanel.bet_4_bg)
                }
                if (num >= bet3 && this.selectBet == this.playPanel.bet_3) {
                    this.playPanel.bet_3.name = "ok"
                    this.playPanel.bet_3_bg.rotation = 0;
                    this.playPanel.bet_3_bg.visible = true;
                    EffectUtils.rotationEffect(this.playPanel.bet_3_bg)
                }
                if (num >= bet2 && this.selectBet == this.playPanel.bet_2) {
                    this.playPanel.bet_2.name = "ok"
                    this.playPanel.bet_2_bg.rotation = 0;
                    this.playPanel.bet_2_bg.visible = true;
                    EffectUtils.rotationEffect(this.playPanel.bet_2_bg)
                }
                if (num >= bet1 && this.selectBet == this.playPanel.bet_1) {
                    this.playPanel.bet_1.name = "ok"
                    this.playPanel.bet_1_bg.rotation = 0;
                    this.playPanel.bet_1_bg.visible = true;
                    EffectUtils.rotationEffect(this.playPanel.bet_1_bg)
                }
                if (num >= bet0 && this.selectBet == this.playPanel.bet_0) {
                    this.playPanel.bet_0.name = "ok"
                    this.playPanel.bet_0_bg.rotation = 0;
                    this.playPanel.bet_0_bg.visible = true;
                    EffectUtils.rotationEffect(this.playPanel.bet_0_bg)
                }
            }
        }
        //金币拆分
        private qiegeJinbi(fen: number): Array<any> {
            let Fen = fen
            let imgList: Array<any> = [];
            let guigeList = this.bet_chip;
            for (let j = guigeList.length; j >= 0; j--) {
                let len = Math.floor(Fen / guigeList[j]);
                if (len != 0) {
                    for (var k = 0; k < len; k++) {
                        imgList.push(guigeList[j]);
                        Fen -= guigeList[j];
                    }
                }
            }
            return imgList
        }
        //生成指定的随机数
        private random_num(min: number, max: number): number {
            let Range = max - min;
            let Rand = Math.random();
            return (min + Math.round(Rand * Range));
        }
        //根据牌值获取牌型
        private getChessType(arr: Array<number>) {
            if (arr == null || arr.length != 2) {
                return
            }
            if (arr[0] == arr[1]) {//豹子
                return 11
            }
            if ((arr[0] == 2 && arr[1] == 8)//28杠
                || (arr[0] == 8 && arr[1] == 2)) {
                return 10
            }
            return (arr[0] + arr[1]) % 10;
        }

        //点击下拉菜单
        private menu_click() {
            if (this.playPanel.menu_button.name == "on") {//收起菜单
                egret.Tween.removeTweens(this.playPanel.menu_button)
                egret.Tween.removeTweens(this.playPanel.menu_group)
                this.playPanel.menu_button.name = "off"
                egret.Tween.get(this.playPanel.menu_button).to({ rotation: 0 }, 300)
                egret.Tween.get(this.playPanel.menu_group).to({ x: -260 }, 300).call((menu_group: eui.Group) => {
                    menu_group.visible = false;
                }, this, [this.playPanel.menu_group])
            } else {                                      //弹出菜单
                egret.Tween.removeTweens(this.playPanel.menu_button)
                egret.Tween.removeTweens(this.playPanel.menu_group)
                this.playPanel.menu_button.name = "on"
                this.playPanel.menu_group.visible = true;
                egret.Tween.get(this.playPanel.menu_button).to({ rotation: 90 }, 300)
                egret.Tween.get(this.playPanel.menu_group).to({ x: 0 }, 300)
            }
        }
        //点击反馈
        private fankuiClick() {
            SoundManage.startAnniuBtnLoad();
            this.menu_click();
            game.AppFacade.instance.sendNotification(PanelNotify.HALL.OPEN_FANKUI, InstructionsEnum.GAME_TYPE_NAME.BREBG)
        }
        //点击记录
        private jiluClick() {
            SoundManage.startAnniuBtnLoad();
            this.menu_click();
            var obj = {
                gameType: InstructionsEnum.GAME_TYPE_NAME.BREBG,
                roomType: InstructionsEnum.LEVEL_GAME
            }
            game.AppFacade.instance.sendNotification(PanelNotify.HALL.OPEN_JILU, JSON.stringify(obj))
        }
        //点击设置
        private setClick() {
            SoundManage.startAnniuBtnLoad();
            this.menu_click();
            game.AppFacade.instance.sendNotification(PanelNotify.HALL.OPEN_SETTING)
        }
        //点击帮助
        private helpClick() {
            SoundManage.startAnniuBtnLoad();
            this.menu_click();
            game.AppFacade.instance.sendNotification(PanelNotify.HALL.OPEN_HELP_BREBG)
        }
        //点击退出
        private exitClick() {
            SoundManage.startAnniuBtnLoad();
            this.menu_click();
            if (BrebgVO.getVo().game_state == 2) {
                let obj = new Object();
                obj[InstructionsEnum.TEXT] = "请等待游戏结束后离开！";
                game.AppFacade.instance.sendNotification(PanelNotify.HALL.OPEN_TIP, obj);
            } else {
                let obj = new Object();
                obj[InstructionsEnum.CODE] = PanelNotify.BREBG.CLOSE_PLAY_GAME;
                obj[InstructionsEnum.TEXT] = "确定要离开游戏吗？";
                game.AppFacade.instance.sendNotification(PanelNotify.HALL.OPEN_TIP, obj);
            }
        }
        //点击玩家列表
        private userListClic() {
            SoundManage.startAnniuBtnLoad();
            game.AppFacade.instance.sendNotification(PanelNotify.HALL.OPEN_USER_LIST, InstructionsEnum.GAME_TYPE_NAME.BREBG)
        }
    }
}